package
{
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLStream;
	import flash.net.URLVariables;
	import flash.system.System;
	import flash.ui.Keyboard;
	
	public class CGameState_Ranking extends CGameState
	{
		private var m_streamRankingData :URLStream = new URLStream();
		
		private var m_refUI :CMC_GameState_Ranking = null;
		private var m_refDataGrid :DataGrid = null;
		
		public function CGameState_Ranking(refGame :CGame)
		{
			super(refGame);
			
			cMovieClip = new CMC_GameState_Ranking();
			
			m_refUI = CMC_GameState_Ranking(cMovieClip);
			m_refDataGrid = m_refUI.datagridRanking;
			
			m_refDataGrid.columns = ["랭크", "이름", "점수", "최대_뭉_길이", "경과_시간", "날짜"];
			m_refDataGrid.sortableColumns = false;
			DataGridColumn(m_refDataGrid.columns[0]).width = 40;
			DataGridColumn(m_refDataGrid.columns[1]).width = 100;
			DataGridColumn(m_refDataGrid.columns[2]).width = 50;
			DataGridColumn(m_refDataGrid.columns[3]).width = 100;
			DataGridColumn(m_refDataGrid.columns[4]).width = 80;
			DataGridColumn(m_refDataGrid.columns[5]).width = 130;
		}
		override public function EnterState() :void
		{
			LoadRankingData("Sort_Date");

			m_refUI.btnSort_Date.addEventListener(MouseEvent.CLICK, OnClickBtn_SortDate);
			m_refUI.btnSort_Mung.addEventListener(MouseEvent.CLICK, OnClickBtn_SortMung);
			m_refUI.btnSort_Score.addEventListener(MouseEvent.CLICK, OnClickBtn_SortScore);
			m_refUI.btnSort_Time.addEventListener(MouseEvent.CLICK, OnClickBtn_SortTime);
			
			m_refUI.btnSearch_Name.addEventListener(MouseEvent.CLICK, OnClickBtn_SearchName);

			cMovieClip.stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			m_refUI.btnOK.addEventListener(MouseEvent.CLICK, OnClickBtn_OK);
		}
		override public function LeaveState() :void
		{
			m_refUI.btnOK.removeEventListener(MouseEvent.CLICK, OnClickBtn_OK);
			cMovieClip.stage.removeEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			
			m_refUI.btnSearch_Name.removeEventListener(MouseEvent.CLICK, OnClickBtn_SearchName);
			
			m_refUI.btnSort_Time.removeEventListener(MouseEvent.CLICK, OnClickBtn_SortTime);
			m_refUI.btnSort_Score.removeEventListener(MouseEvent.CLICK, OnClickBtn_SortScore);
			m_refUI.btnSort_Mung.removeEventListener(MouseEvent.CLICK, OnClickBtn_SortMung);
			m_refUI.btnSort_Date.removeEventListener(MouseEvent.CLICK, OnClickBtn_SortDate);
			
			m_refDataGrid.removeAll();
		}
		
		private function GotoTitle() :void
		{
			m_refGame.EnterState(CGameState.GAME_STATE_TITLE);
			
			CSoundManager.instance.PlaySound(CSoundManager.SOUND_7_EAT);
		}
		private function LoadRankingData(strSelectMode :String) :void
		{
			m_refDataGrid.removeAll();
			
			var variables :URLVariables = new URLVariables();
			variables.SelectMode = strSelectMode;
			if (strSelectMode == "Search_Name")
			{
				variables.SearchName = m_refUI.textName.text;
			}
			var request :URLRequest = new URLRequest("./AZSnake_Select_Ranking.php");
			request.method = URLRequestMethod.POST;
			request.data = variables;
			
			m_streamRankingData.addEventListener(Event.COMPLETE, OnLoadingComplete);
			
			try	{ m_streamRankingData.load(request); }
			catch (error:Error) { m_refUI.textDebug.appendText("Unable to load URL: " + error); }
		}
		
		private function OnLoadingComplete(event :Event) :void
		{
			var strRanking :String = m_streamRankingData.readMultiByte(m_streamRankingData.bytesAvailable, "utf-8");
			var xmlRanking :XMLList = XMLList(strRanking);
			
			for (var i :uint = 0; i < xmlRanking.Data.length(); ++i)
			{
				var nRank :uint = i + 1;
				
				m_refDataGrid.addItem({
				랭크:nRank.toString(),
				이름:xmlRanking.Data[i].@Name.toString(),
				점수:xmlRanking.Data[i].@Score.toString(),
				최대_뭉_길이:xmlRanking.Data[i].@HighestMungLength.toString(),
				경과_시간:xmlRanking.Data[i].@ElapsedTime.toString(),
				날짜:xmlRanking.Data[i].@TimeStamp});
			}
			
			m_streamRankingData.removeEventListener(Event.COMPLETE, OnLoadingComplete);
		}
		
		private function OnClickBtn_SortDate(event :MouseEvent) :void
		{
			LoadRankingData("Sort_Date");
		}
		private function OnClickBtn_SortTime(event :MouseEvent) :void
		{
			LoadRankingData("Sort_Time");
		}
		private function OnClickBtn_SortScore(event :MouseEvent) :void
		{
			LoadRankingData("Sort_Score");
		}
		private function OnClickBtn_SortMung(event :MouseEvent) :void
		{
			LoadRankingData("Sort_Mung");
		}
		private function OnClickBtn_SearchName(event :MouseEvent) :void
		{
			if (m_refUI.textName.text == "") return;
			
			LoadRankingData("Search_Name");
		}
		private function OnClickBtn_OK(event :MouseEvent) :void
		{
			GotoTitle();
		}
		private function OnKeyDown(event :KeyboardEvent) :void
		{
			if (event.keyCode == Keyboard.ENTER)
			{
				GotoTitle();
			}
		}

	}
}